<template>
  <div class="login-container">
    <el-row style="width: 100%;
    padding: 10em 10px 5em 10px;">
      <el-col :md="{ span: 10 }" :lg="{ span: 6}" :xs="{ span: 24}" :sm="{ span: 14}"
              style='max-width:420px;float:right;margin-right:5%'>
        <el-card class="login-form">
          <div
            slot="header"
            style="color: #2b3d51; font-weight: 600; font-size: 15px"
          >
            欢迎登陆
          </div>

          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            auto-complete="on"
            label-position="left"
          >
            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user"/>
              </span>
              <el-input
                v-model="loginForm.username"
                name="username"
                type="text"
                auto-complete="on"
                placeholder="用户名"
              />
            </el-form-item>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password"/>
              </span>
              <el-input
                :type="pwdType"
                v-model="loginForm.password"
                name="password"
                auto-complete="on"
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon
                  :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'"
                />
              </span>
            </el-form-item>
            <el-form-item>
              <div style="background: #fff">
                <el-row>
                  <el-col :span="16" style="padding-left: 5px">
                    <el-radio v-model="savePwd" label="记住密码"
                    >记住密码
                    </el-radio
                    >
                  </el-col>
                  <el-col :span="8" style="text-align: right">
                    <el-button
                      class="btn"
                      :loading="loading"
                      type="primary"
                      style="
                        width: 100%;
                        padding: 10px 15px;
                        border-radius: 25px;
                        font-size: 14px;
                      "
                      @click.native.prevent="handleLogin"
                    >
                      登录 Log In
                    </el-button>
                  </el-col>
                </el-row>
              </div>
            </el-form-item>
            <!-- <div class="tips">
        <span style="margin-right: 20px">账号: admin</span>
        <span> 密码: admin</span>
      </div> -->
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import md5 from 'js-md5'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          this.$RegExp.required()
        ],
        password: [
          this.$RegExp.required()
        ]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined,
      savePwd: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('Login', Object.assign({password:md5(this.loginForm.password),username:this.loginForm.username}))
            .then(() => {
              this.loading = false
              this.$router.push({ path: this.redirect || '/' })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;

    input {
      height: 40px;
      background: transparent;
      border: unset;
      color: #000;
      font-size: 16px;
    }
  }

  .el-form-item {
    border-radius: 20px;
    background: #e8f0fe;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url("../../assets/img/login-bg.jpg") no-repeat center;
  background-size: 100% 100%;

  .login-form {
    background: #fff;
  }

  .tips {
    font-size: 14px;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #c8c8c8;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

/deep/ .el-card {
  border-radius: 15px;
}

/deep/ .el-radio__label {
  color: #2b3d51;
  font-size: 16px;
}
</style>
